package com.peng.community.service.impl;

import com.peng.community.entity.Message;
import com.peng.community.mapper.MessageMapper;
import com.peng.community.util.SensitiveFilter;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.List;

@Service
public class MessageService {

    @Autowired
    MessageMapper messageMapper;

    //查询消息列表窗口的全部消息（就是消息列表，只显示最后一条）
    public List<Message> selectConversations(int userId, int offset, int limit) {
        return messageMapper.selectConversations(userId,offset, limit);
    }

    //查询消息列表的未读消息总数
    public int selectConversationsCount(int userId) {
        return messageMapper.selectConversationsCount(userId);
    }

    //查询会话窗口的消息（就是对话框的消息）
    public List<Message> selectLetters(String conversationId, int offset, int limit) {
        return messageMapper.selectLetters(conversationId,offset, limit);
    }

    //查询会话窗口会话总数
    public int selectLetterCount(String conversationId) {
        return messageMapper.selectLetterCount(conversationId);
    }

    //查询未读消息总数
    //若传递conversationId，就专门查询这个对话的未读数量，若不传递，就查询所有的未读消息
    public int selectLetterUnreadCount(int userId, String conversationId) {
        return messageMapper.selectLetterUnreadCount(userId, conversationId);
    }

    @Autowired
    SensitiveFilter sensitiveFilter;

    //新增一条消息
    public int insertMessage(Message message) {
        //对敏感词和标签进行过滤
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveFilter.filter(message.getContent()));
        return messageMapper.insertMessage(message);
    }

    //将多条消息标记为已读(1)
    public int readMessages(List<Integer> messageIds) {
        return messageMapper.updateStatus(messageIds, 1);
    }


    //查询最新的系统通知 (int userId, String topic)
    public Message findLatestNotice(int userId, String topic) {
        return messageMapper.findLeastNotice(userId,topic);
    }

    //查询系统通知总数量
    public int findNoticeCount(int userId, String topic) {
        return messageMapper.findNoticeCount(userId,topic);
    }

    //查询未读的系统通知数量
    public int findNoticeUnreadCount(int userId, String topic) {
        return messageMapper.findNoticeUnreadCount(userId,topic);
    }

    public List<Message> findNotices(int userId, String topic, int offset, int limit) {
        return messageMapper.findNotices(userId,topic,offset,limit);
    }

}
